<template>
  <div id="create">
    <!-- 修改用户的对话框 -->
    <el-dialog title="修改用户" :visible.sync="dialog" :before-close="closeDialog" width="35%" min-width="300px">
      <el-form ref="editFormRef" :rules="editFormRules" :model="editForm" label-position="right" label-width="auto" style="padding: 0 60px;">
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="editForm.roleName" />
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="editForm.roleDesc" />
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">
          取消
        </el-button>
        <el-button type="primary" @click="confirmDialog">
          确定
        </el-button>
      </div>
    </el-dialog>

  </div>

</template>

<script>
export default {
  name: 'EditItem',
  props: {
    // 组件接收父级传递的控制弹框 打开/关闭的状态值
    dialog: {
      type: Boolean,
      default: null
    },
    value: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      // 查询到的用户信息对象
      editForm: {
        roleId: this.value.roleId,
        roleDesc: this.value.roleDesc,
        roleName: this.value.roleName
      },
      // 修改表单的验证规则对象
      editFormRules: {
        roleName: [
          { required: true, message: '请输入角色名称', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur'
          }
        ],
        roleDesc: [
          { required: true, message: '请输入角色描述', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur'
          }
        ]
      }
    }
  },

  methods: {
    // 点击取消
    closeDialog() {
      this.$emit('close')
    },
    // 点击确认，处理提交表单，向父级传递
    confirmDialog() {
      this.$refs['editFormRef'].validate((valid) => {
        if (valid) {
          // 定义一个变量为 editData 用来接收拷贝的 this.temp
          const editData = Object.assign({}, this.editForm)
          // console.log(editData)，打印看里面的字段
          this.$emit('edit', editData)
        }
      })
    }
  }
}
</script>
